package com.ruicar.afs.cloud.risk.tortoise.components;

;
import com.ruicar.afs.cloud.risk.tortoise.dto.notice.GwtTortoiseReq;
import com.ruicar.afs.cloud.risk.tortoise.dto.response.GwtTortoiseResponse;
import com.ruicar.afs.cloud.risk.api.enums.tortoise.GwtTortoiseScene;
import com.ruicar.afs.cloud.risk.tortoise.processor.GwtTortoiseQueryAndNoticeProcessor;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Description
 * @Author Peng Jinbo
 * @Date 2020/07/29 20:22
 */
@Component
@AllArgsConstructor
@Slf4j
public class GwtTortoiseProcessors {

    /**
     * Query and notice processor list
     */
    private final List<GwtTortoiseQueryAndNoticeProcessor> queryAndNoticeProcessorList;
    /**
     * Processor map
     */
    private final Map<GwtTortoiseScene, GwtTortoiseQueryAndNoticeProcessor> processorMap;

    /**
     * Gwt tortoise str redis
     */
    private GwtTortoiseStrRedis gwtTortoiseStrRedis;

    /**
     * Author Peng Jinbo
     * Process gwt tortoise response
     *
     * @param req notice
     * @return the gwt tortoise response
     */
    public GwtTortoiseResponse process(GwtTortoiseReq req) {
        if (Objects.isNull(req.getOperationType())) return GwtTortoiseResponse.err("非法操作类型");
        if (Objects.isNull(req.getSceneCode())) return GwtTortoiseResponse.err("非法场景编号");
        GwtTortoiseResponse response;
        switch (req.getOperationType()) {
            case QUERY_DATA:
                log.info("玄武数据拉取 req：{}",req);
                response = processorMap.get(req.getSceneCode()).query(req);
                log.info("玄武数据拉取 resp：{}",response);
                break;
            case RESULT_NOTICE:
                log.info("玄武结果通知 req : {}",req);
                response = processorMap.get(req.getSceneCode()).notice(req);
                gwtTortoiseStrRedis.delayClear(req);
                log.info("玄武结果通知 resp : {}",response);
                break;
            default:
                response = GwtTortoiseResponse.err(req.getSceneCode().name());
        }
        return response;
    }

    /**
     * Author Peng Jinbo
     * Init
     */
    @PostConstruct
    public void init() {
        queryAndNoticeProcessorList.stream().forEach(process -> {
            for (GwtTortoiseScene gwtTortoiseScene : process.belongScene()) {
                processorMap.put(gwtTortoiseScene, process);
            }
        });
    }
}
